package com.jiantu.controller;

import com.jiantu.common.PageBean;
import com.jiantu.common.R;
import com.jiantu.domain.dto.PageDTO;
import com.jiantu.domain.dto.StudentDTO;
import com.jiantu.domain.pojo.Students;
import com.jiantu.service.StudentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *学生信息管理
 */
@RestController
@RequestMapping("/students")
@CrossOrigin
public class StudentsController {
    @Autowired
    private StudentsService studentsService;

    @GetMapping("/{id}")
    public Students findById( @PathVariable("id") Integer id) {
        return studentsService.findById(id);
    }

    @GetMapping("/getStudentList")
    public R findByPage(PageDTO pageDTO) {
        PageBean studentList = studentsService.findByPage(pageDTO);
        return R.success(studentList);
    }

    @PostMapping("/addStudent")
    public R insert(@RequestBody Students students) {

        int insert = studentsService.insert(students);
        if (insert >=1){
            return R.success();
        }
        return R.error("添加失败");
    }

    @PostMapping("/updateStudent")
    public R update(@RequestBody StudentDTO studentDTO) {

        int update = studentsService.update(studentDTO);
        if (update >=1){
            return R.success();
        }
        return R.error("更新失败");
    }

    @DeleteMapping("/deleteStudentById/{id}")
    public R deleteById(@PathVariable("id") Integer id) {

        int del = studentsService.deleteById(id);
        if (del >=1){
            return R.success();
        }
        return R.error("删除失败");
    }
}
